﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace DuJianliang.ApplicationPlatform.Flow
{
    public partial class FlowRejectStepSelect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //string strFlowInstanceID = Request.QueryString["FlowInstanceID"];
                //Guid guidFlowInstanceID = string.IsNullOrWhiteSpace(strFlowInstanceID) ? Guid.Empty : new Guid(strFlowInstanceID);
                //BindRadioButtonList(guidFlowInstanceID);

                string flowID = Request.QueryString["FlowID"];
                string currentStepID = Request.QueryString["StepID"];
                radioButtonList.Items.Clear();
                BindRadioButtonList(flowID, currentStepID);
            }  
        }

//        private void BindRadioButtonList(Guid flowInstanceID)
//        {
//            string sql = @"SELECT DISTINCT FlowInstanceID, StepID, StepName
//                            FROM dbo.FlowInstanceStep WHERE FlowInstanceID = @FlowInstanceID AND Completed = 1
//                            ORDER BY StepID";

//            Database database = DatabaseFactory.CreateDatabase();
//            DbCommand dbCommand = database.GetSqlStringCommand(sql);
//            database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.Guid, flowInstanceID);
//            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

//            foreach (DataRow dataRow in dataTable.Rows)
//            {
//                ListItem item = new ListItem(dataRow["StepName"].ToString(), dataRow["StepID"].ToString());
//                radioButtonList.Items.Add(item);
//            }
//            if (radioButtonList.Items.Count > 0)
//            {
//                radioButtonList.SelectedIndex = 0;
//            }
//        }

        private void BindRadioButtonList(string flowID, string currentStepID)
        {
            try
            {
                DataTable dataTable = GetFlowStepRelatio(flowID);

                DataTable dataSource = new DataTable();
                dataSource.Columns.Add("Text");
                dataSource.Columns.Add("Value");

                SetRadioButtonList(dataTable, currentStepID, dataSource);                
                DataRow[] dataRows = dataSource.DefaultView.ToTable(true).Select("", "Value");
                foreach (DataRow dataRow in dataRows)
                {
                    ListItem item = new ListItem(dataRow["Text"].ToString(), dataRow["Value"].ToString());
                    radioButtonList.Items.Add(item);
                }
                if (radioButtonList.Items.Count > 0)
                {
                    radioButtonList.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void SetRadioButtonList(DataTable dataTableFlowStepRelatio, string stepID, DataTable radioButtonListDataSource)
        {
            DataRow[] dataRows = dataTableFlowStepRelatio.Select(string.Format("StepID='{0}'", stepID));
            foreach (DataRow dataRow in dataRows)
            {
                string parentID = dataRow["ParentID"].ToString();
                string parentStepName = dataRow["StepName"].ToString();

                if (string.IsNullOrWhiteSpace(parentID))
                {
                    break;
                }

                ListItem listItem  = radioButtonList.Items.FindByValue(parentID);
                if (listItem != null)
                {
                    break;
                }

                radioButtonListDataSource.Rows.Add(parentStepName, parentID);

                SetRadioButtonList(dataTableFlowStepRelatio, parentID, radioButtonListDataSource);
            }
        }

        private DataTable GetFlowStepRelatio(string flowID)
        {
            string sql = @"SELECT A.StepID, A.ParentID, B.StepName FROM dbo.FlowStepRelation A
                            LEFT JOIN dbo.FlowStep B ON A.ParentID = B.StepID
                            WHERE A.FlowID = @FlowID";
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@FlowID", DbType.String, flowID);
                DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];
                return dataTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}